//处理点击事件的延时问题
var scriptCom = document.createElement("script");
scriptCom.src = 'js/fastclick.js';
document.getElementsByTagName("head")[0].appendChild(scriptCom);
document.addEventListener('DOMContentLoaded', function() {
	setTimeout(function() {
		FastClick.attach(document.body);
	}, 200);
}, false);

//验证手机号正则
var _checkMobile = function(sMobile) {
	if (!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(sMobile))) {
		return false;
	} else {
		return true;
	}
}
//判断输入的是小数或整数
var _ifNum = function(val, type) {
	// 返回true:满足   false:不满足
	// type:int/float
	if (!parseFloat(val)) {
		return false;
	}
	var str = String(val);
	var point = 0;
	var off = true;
	for (var i = 0; i < str.length; i++) {
		if (isNaN(Number(str[i]))) {
			//不是数字
			if (str[i] == '.') {
				point++;
			} else {
				off = false;
				break;
			}
		}
	}
	if (type == 'int') {
		(off && !point) ? '' : off = false;
	}
	if (type == 'float') {
		(off && point <= 1 && str[str.length - 1] != '.' && str[0] != '.') ? '' : off = false;
	}
	return off;
};
//转换时间戳
var _date = function(timestamp) {
	if (!timestamp) {
		return "";
	}
	var date;
	//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	if (String(timestamp).length == 10) {
		date = new Date(Number(timestamp) * 1000);
	}
	if (String(timestamp).length == 13) {
		date = new Date(Number(timestamp));
	}
	var Y = date.getFullYear() + "-";
	var M =
		(date.getMonth() + 1 < 10 ?
			"0" + (date.getMonth() + 1) :
			date.getMonth() + 1) + "-";
	var D = date.getDate() + " ";
	var h = date.getHours() + ":";
	if (date.getHours() < 10) {
		h = '0' + date.getHours() + ":";
	}
	var m = date.getMinutes() + ":";
	if (date.getMinutes() < 10) {
		m = '0' + date.getMinutes() + ":";
	}
	var s = date.getSeconds();
	if (date.getSeconds() < 10) {
		s = '0' + date.getSeconds();
	}
	return Y + M + D + h + m + s;
}

//钱分转换为元
var _money = function(val) {
	var judge = String(val);
	if (judge == "undefined" || judge == "null") {
		return 0.0;
	}
	var priceStr = String(parseFloat(parseInt(val) / 100));
	var index = priceStr.indexOf(".");
	if (index != -1) {
		var handle = priceStr.slice(index + 1);
		if (handle.length == 1) {
			priceStr = priceStr + "0";
		}
	} else {
		priceStr = priceStr + ".00";
	}
	return priceStr;
}
/*
	content:密码错误(提示内容)
	position:center(提示的垂直位置)
*/
var _toast = function(content, position) {
	if (!position) {
		position = 'center';
	}
	plus.nativeUI.toast(content, {
		verticalAlign: position
	});
}

// 复制到剪贴板
var _copy = function(text) {
	var osName = plus.os.name;
	if (plus.os.name == "Android") {
		var Context = plus.android.importClass("android.content.Context");
		var main = plus.android.runtimeMainActivity();
		var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
		plus.android.invoke(clip, "setText", text);
	} else if (osName == "iOS") {
		var UIPasteboard = plus.ios.importClass("UIPasteboard");
		var generalPasteboard = UIPasteboard.generalPasteboard();
		// 设置/获取文本内容:
		generalPasteboard.setValueforPasteboardType(text, "public.utf8-plain-text");
	}
};

//加法
var _accAdd=function(arg1,arg2){ 
    var r1,r2,m; 
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0} 
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0} 
    m=Math.pow(10,Math.max(r1,r2)) 
    return (arg1*m+arg2*m)/m 
}; 

//减法
var _accSub=function(arg1,arg2){ 
       var r1,r2,m,n; 
       try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0} 
       try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0} 
       m=Math.pow(10,Math.max(r1,r2)); 
       //last modify by deeka 
       //动态控制精度长度 
       n=(r1>=r2)?r1:r2; 
       return ((arg1*m-arg2*m)/m).toFixed(n); 
}

//除法
var _accDiv = function(arg1, arg2) {
	var t1 = 0,
		t2 = 0,
		r1, r2;
	try {
		t1 = arg1.toString().split(".")[1].length
	} catch (e) {}
	try {
		t2 = arg2.toString().split(".")[1].length
	} catch (e) {}
	with(Math) {
		r1 = Number(arg1.toString().replace(".", ""))
		r2 = Number(arg2.toString().replace(".", ""))
		return (r1 / r2) * pow(10, t2 - t1);
	}
};

//乘法
var _accMul = function(arg1, arg2) {
	var m = 0,
		s1 = arg1.toString(),
		s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length
	} catch (e) {}
	try {
		m += s2.split(".")[1].length
	} catch (e) {}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
};

//截取6位
var _toFixed = function(str1, num) {
	var str = String(str1);

	function hanZeo(z) {
		var zeo = '';
		for (var i = 0; i < z; i++) {
			zeo += '0';
		}
		return zeo;
	}
	var arr = str.split('.');
	if (arr[1]) {
		if (arr[1].length >= num) {
			return arr[0] + '.' + arr[1].slice(0, num);
		} else {
			return arr[0] + '.' + arr[1] + hanZeo(num - arr[1].length);
		}
	} else {
		return str + '.' + hanZeo(num);
	}
}

//关闭除index的其他页面
var _clsoeOther = function() {
	function plusReady() {
		var index = plus.webview.getLaunchWebview();
		var all = plus.webview.all();
		var indexUrl = index.getURL();
		for (var i = 0; i < all.length; i++) {
			if (all[i].getURL() != indexUrl) {
				all[i].hide();
			}
		}
		setTimeout(function() {
			for (var i = 0; i < all.length; i++) {
				if (all[i].getURL() != indexUrl) {
					all[i].close('none');
				}
			}
		}, 1500);
	}
	// 判断扩展API是否准备，否则监听'plusready'事件
	if (window.plus) {
		plusReady();
	} else {
		document.addEventListener('plusready', plusReady, false);
	}
};

//请求
var _post = function(json) {
	console.log('url++++++' + json.url);
	console.log('post参数+++++++++');
	console.log(JSON.stringify(json.data));
	$.post(json.url, {
		'json': json.noEncrypt?JSON.stringify(json.data):encrypt(JSON.stringify(json.data))
	}, function(data) {
		if(data.code=='501'){
			//token无效，重新登录
			setTimeout(function() {
				_toast("身份验证失败，请重新登录");
				setTimeout(function(){
					localStorage.removeItem("token");
					localStorage.removeItem("encrypt");
					localStorage.removeItem("decrypt");
					_openHref({
						url: 'login.html',
						id: 'login'
					});
				},2500);
			}, 500);
			return;
		}
		try{
			data.data=JSON.parse(decrypt(data.data));
		}catch(e){
		}
		json.success(data);
		console.log('返回数据');
		console.log(JSON.stringify(data))
	})
};

//简单加密算法
var _simpleEncrypt = function(val) {
	var PWD = 'ILoveChina';
	var IV = 'YesYouAr';
	var encrypted = CryptoJS.DES.encrypt(val, CryptoJS.enc.Utf8.parse(PWD), {
		iv: CryptoJS.enc.Utf8.parse(IV),
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	})
	return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}

//简单的解密算法
var _simpleDecrypt = function(val) {
	var PWD = 'ILoveChina';
	var IV = 'YesYouAr';
	var encrypted = CryptoJS.DES.decrypt({
		ciphertext: CryptoJS.enc.Base64.parse(val)
	}, CryptoJS.enc.Utf8.parse(PWD), {
		iv: CryptoJS.enc.Utf8.parse(IV),
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	})
	return encrypted.toString(CryptoJS.enc.Utf8);
}

//简单加密的post请求
var _simplePost = function(json) {
	console.log('url++++++' + json.url);
	console.log('post参数+++++++++');
	console.log(JSON.stringify(json.data));
	var obj = new XMLHttpRequest();
	obj.open('post', json.url, true);
	obj.onreadystatechange = function() {
		if (obj.readyState == 4 && obj.status == 200 || obj.status == 304) {
			var ret = JSON.parse(obj.responseText);
			console.log("post返回的结果++++++" + JSON.stringify(ret));
			json.success(ret);
		}
	};
	var simStr = _simpleEncrypt(JSON.stringify(json.data));
	var randomStr = ranStr(12);
	var realStr = simStr.slice(0, 12) + randomStr + simStr.slice(12);
	obj.send(realStr);
};
//打开页面
var _openHref = function(json) {
	var animate = json.animate ? json.animate : 'slide-in-right';
	var op = {
		// 'backButtonAutoControl': 'close', 
		// 'animationOptimization': 'none', //不优化
	};
	if (plus.os.name.toLowerCase() == 'android' && parseFloat(plus.os.version) < 6) {
		op.hardwareAccelerated = json.spe ? json.spe : false;
	}
	var web = plus.webview.getWebviewById(json.id);
	if (web) {
		web.show(animate);
	} else {
		app.$data.jiazai = true;
		web = plus.webview.create(json.url, json.id, op);
		web.addEventListener("loaded", function() {
			setTimeout(function() {
				app.$data.jiazai = false;
				web.show(animate);
			}, 200);
		}, false);
	}
	return web;
};

//域名  正式
var HTTP = 'http://192.168.43.107:9001';

//登录发送短信
var loginSendCodeUrl = HTTP + '/merchant/sendNote';

//登录接口
var loginUrl = HTTP + '/merchant/login'

//每次进app刷新token
var refreshTokenUrl=HTTP+'/merchant/refresh'

//得到所有没出餐的订单
var getNoNoodelUrl=HTTP+'/order/getNoNoodel';

//得到已出餐的订单
var getYesNoodelUrl=HTTP+'/order/getYesNoodel';

//获取订单详情
var getOrderInfoUrl=HTTP+'/order/getOrderInfoUrl'
